class Solution(object):
    def countQuadruples(self, firstString, secondString):
        d = {}
        for index, c in enumerate(secondString):
            d[c] = index
        min_dist = float('inf')
        min_ans = 0
        for index, c in enumerate(firstString):
            if c in d and index - d[c] < min_dist:
                min_dist = index - d[c]
                min_ans = 1
            else:
                if c in d and index - d[c] == min_dist:
                    min_ans += 1
        return min_ans